<%@ page language="java" pageEncoding="us-ascii" contentType="text/html;charset=us-ascii" %>
<%--Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
	 
	This file is part of CDinFusion.
	
	CDinFusion is free software: you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public 
	License in version 3 (LGPL3) as published by the Free Software
	Foundation version 3 of the License.
	
	CDinFusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	Lesser General Public License for more details.
	
	You should have received a copy of the Lesser General Public
	License along with CDinFusion; see the file COPYING. If not,
	write to the Free Software Foundation, 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
	
	If you have any questions, send an email to: megx@mpi-bremen.de.
--%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.cdinfusion.data.*"%>
<%@ page import="org.cdinfusion.data.environment.*"%>
<%@ page import="org.cdinfusion.data.studytype.*"%>
<%@ page import="java.util.*"%>

<%System.out.println("<<<<<<<<<<3a_CD_INPUT_1to1>>>>>>>>>>");%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<style type="text/css">
</style>
<title> DIRECT CONTEXTUAL DATA INPUT 1 OR n TO 1 </title> 
<jsp:include page="googleanalytics.jsp"/>

<script type="text/javascript" src="/CDinFusion/js/numValues.js"></script>
</head>

<body>
<table border="0" width="100%" bgcolor="#2B3990" cellpadding=0 cellspacing=0>
<tr bgcolor="#ffffff">
<td width=250>
<center>
<img height="81" width="250" alt="CDinFusion Logo" src="/CDinFusion/img/logo_small_dark.png">
</center>
</td>
</tr>

<tr align="left">    
<td>
<jsp:include page="documentation/navigation.jsp"></jsp:include>
</td>
</tr>

<%
String message = "";
String seqID = "";// (String) session.getAttribute("seqID");
String sessionID = request.getSession().getId();
String origin = (String) session.getAttribute("origin");

String study ="";
String env ="";

Object obj1 = null;
Object obj2 = null;
Object obj3 = null;

boolean error = false;
boolean keepheader = false;
boolean old_keepheader = false;
boolean fillEnv = false;
boolean fillStudy = false;
boolean fillFasta = false;
String context_exists = (String)session.getAttribute("context_exists");

List<CDElement> cdeLIST = null;


try{

if(origin.equals("3a_csv")){
	cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
	
	study = cdeLIST.get(0).getStudy().getClass().toString().substring(36).toLowerCase().trim();
	env = cdeLIST.get(0).getEnv().getClass().toString().substring(38).toLowerCase().trim();
	
	CDElement cdelement = cdeLIST.get(0);
	keepheader = cdelement.isKeepheader();
	seqID = cdeLIST.get(0).getFirstSequenceID();
	obj1 = cdelement.getStudy();
	obj2 = cdelement.getEnv();
	obj3 = cdelement.getCDFastaheader();
	//TODO: make it more specific
	fillEnv = true;
	fillStudy = true;
	fillFasta = true;
}

if(origin.equals("uploadcsv")){
	cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
	study = cdeLIST.get(0).getStudy().getClass().toString().substring(36).toLowerCase().trim();
	env = cdeLIST.get(0).getEnv().getClass().toString().substring(38).toLowerCase().trim();
	CDElement cdelement = cdeLIST.get(0);
	keepheader = cdelement.isKeepheader();
	seqID = cdeLIST.get(0).getFirstSequenceID();
	obj1 = cdelement.getStudy();
	obj2 = cdelement.getEnv();
	obj3 = cdelement.getCDFastaheader();
	//TODO: make it more specific
	fillEnv = true;
	fillStudy = true;
	fillFasta = true;
}

if(context_exists.equals("no") & !origin.equals("uploadcsv")){
	cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
	study = cdeLIST.get(0).getStudy().getClass().toString().substring(36).toLowerCase().trim();
	env = cdeLIST.get(0).getEnv().getClass().toString().substring(38).toLowerCase().trim();
	seqID = cdeLIST.get(0).getFirstSequenceID();
}
if(origin.equals("5")){//exist & 
	cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
	study = cdeLIST.get(0).getStudy().getClass().toString().toLowerCase().substring(36).trim();
	env = cdeLIST.get(0).getEnv().getClass().toString().toLowerCase().substring(38).trim();
	
	List<CDElement> oldLIST = cdeLIST;
	CDElement oldelement = oldLIST.get(0);
	keepheader = oldelement.isKeepheader();
	obj1 = oldelement.getStudy();
	obj2 = oldelement.getEnv();
	obj3 = oldelement.getCDFastaheader();
	seqID = oldelement.getFirstSequenceID();
	System.out.println("seqID: "+seqID);
	fillEnv = true;
	fillStudy = true;
	fillFasta = true;
}

if(context_exists.equals("yes") & origin.equals("2a")){
	List<CDElement> oldLIST = (List<CDElement>) session.getAttribute("oldLIST");
	String old_study = oldLIST.get(0).getStudy().getClass().toString().substring(36).toLowerCase().trim();
	String old_env = oldLIST.get(0).getEnv().getClass().toString().substring(38).toLowerCase().trim();
	CDElement oldelement = oldLIST.get(0);
	keepheader = oldelement.isKeepheader();
	obj1 = oldelement.getStudy();
	obj2 = oldelement.getEnv();
	obj3 = oldelement.getCDFastaheader();
	
	cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
	study = cdeLIST.get(0).getStudy().getClass().toString().toLowerCase().substring(36).trim();
	env = cdeLIST.get(0).getEnv().getClass().toString().toLowerCase().substring(38).trim();

	seqID = cdeLIST.get(0).getFirstSequenceID();
	if(study.equals(old_study)) fillStudy = true;
	if(env.equals(old_env)) fillEnv = true;
	if(study.equals(old_study) | env.equals(old_env)){
		fillFasta = true;
	}else{
		fillFasta = false;
	}
	if(old_keepheader == true) keepheader = true;
}
}catch(Exception e){
	System.out.println("Exception in 3a: "+e.getMessage()+" "+e.toString());
	System.out.println("Exception in 3a: "+e.getLocalizedMessage());
}
session.setAttribute("origin", "3a");
%>

<tr bgcolor="#ffffff">
<td>
<div class="center_div">    
<br>
<center><b> Enter contextual data</b> <br>
</center>

<form name="Formular" action="CD_entered_one.htm" method="post" accept-charset="us-ascii" onsubmit="return numValues();">
	<center>
	<table>
		<tr><td>
		<p>sequence identifier: <strong><%out.print("" + seqID);%></strong><p>
	<%try{ %>
		<table style="100%">
  			<tr>
		    <td>
		    <div style="overflow:auto; height: 400px;">
		    
			<%
		if(fillFasta){
			CDFastaHeader cdfheader = (CDFastaHeader) obj3;
			%>
			<b>information, that will be included to the FASTA header line:</b>
			<table width="100%" border="0" bgcolor="#DCDCDC" cellpadding="1">
			
			<tr>
			<td><b><font size=2>parameter</font></b></td>
			<td style="min-width:530px"><b><font size=2></font></b></td>
			<td><b><font size=2>value</font></b></td>
			</tr>
			
			<tr><td><b><font size=1>authority: </font></b></td>
		    <td><font size=1>The author or authors of the organism name from which sequence was obtained.</font></td>
			<td align="right"><input type="text" name="authority" class="required parameter" size="25" value="<%=cdfheader.getAuthority()%>"></td></tr>
			
			<tr><td><b><font size=1>collected by: </font></b></td>
		    <td><font size=1>Name of person who collected sample. Do not use accented or non-ASCII characters.</font></td>
			<td align="right"><input type="text" name="collectedby" class="required parameter" size="25" value="<%=cdfheader.getCollectedby()%>"></td></tr>
			
			<tr><td><b><font size=1>organism name: </font></b></td>
			<td><font size=1>taxonomic name of the sequenced organism, if unknown, e.g. <br> uncultured bacterium, uncultured archaeon, uncultured eukaryote, or uncultured organism</font></td>
			<td align="right"><input type="text" name="organism" class="required parameter" size="25" value="<%=cdfheader.getOrganism()%>"></td></tr>
				
			<tr><td><b><font size=1>isolation source: </font></b></td>
		    <td><font size=1>name of the sampling site/geographic location.</font></td>
			<td align="right"><input type="text" name="isolation_source" class="required parameter" size="25" value="<%=cdfheader.getIsolation_source()%>"></td></tr>
			
			</table><%
		}//end if(fillFasta)
		if(!fillFasta){%>
			<b>information, that will be included to the FASTA header line:</b>
			<table width="100%" border="0" bgcolor="#DCDCDC" cellpadding="1">
			
			<tr>
			<td><b><font size=2>parameter</font></b></td>
			<td style="min-width:530px"><b><font size=2></font></b></td>
			<td><b><font size=2>value</font></b></td>
			</tr>
			<tr><td><b><font size=1>authority: </font></b></td>
		    <td><font size=1>The author or authors of the organism name from which sequence was obtained.</font></td>
			<td align="right"><input type="text" name="authority" class="required parameter" size="25" value=""></td></tr>
			
			<tr><td><b><font size=1>collected by: </font></b></td>
		    <td><font size=1>Name of person who collected sample. Do not use accented or non-ASCII characters.</font></td>
			<td align="right"><input type="text" name="collectedby" class="required parameter" size="25" value=""></td></tr>
			
			<tr><td><b><font size=1>organism name: </font></b></td>
			<td><font size=1>taxonomic name of the sequenced organism, if unknown, e.g. <br> uncultured bacterium, uncultured archaeon, uncultured eukaryote, or uncultured organism</font></td>
			<td align="right"><input type="text" name="organism" class="required parameter" size="25" value=""></td></tr>
			
			<tr><td><b><font size=1>isolation source: </font></b></td>
	   		<td><font size=1>name of the sampling site/geographic location.</font></td>
			<td align="right"><input type="text" name="isolation_source" class="required parameter" size="25" value=""></td></tr>

			</table><%
		}//end if(!fillFasta)
			
		//<!-- studytype -->
		if(fillStudy){
			session.setAttribute("study", study);
			session.setAttribute("obj1", obj1);
			%>
			<jsp:include page="3_filled_study_input_fields.jsp"/>
			<%
		} //end if(fillStudy)
		if (!fillStudy){
			%>
			<jsp:include page="3_empty_study_input_fields.jsp" >
				<jsp:param name="study" value="<%=study%>" />
			</jsp:include>
			<%
		} //end if (!fillStudy)
		
//<!-- env_packages --> 		  
		if(fillEnv){
				session.setAttribute("obj2", obj2);
				session.setAttribute("env", env);
				%>
				<jsp:include page="3_filled_env_input_fields.jsp" />
				<%
			} //end if(fillEnv)
			if(!fillEnv){
				%>
				<jsp:include page="3_empty_env_input_fields.jsp" >
					<jsp:param name="env" value="<%=env%>" />
				</jsp:include>
				<%
			} //end if(!fillEnv)
			%>
			</div>
			</td>
			</tr>
			<tr>
			    <td>Would you like to get a new FASTA header?
				<select name="KeepFASTAHeader" size="1"> 
      			<%
      			if(keepheader == true){%>
      				<option selected value="true">no</option>
      			<%}
      			else{%>
      				<option value="true">no</option>
      			<%}
      			if(keepheader == false){%>
      				<option selected value="false">yes</option>
      			<%}
      			else{%>
      				<option value="false">yes</option>
      			<%}%>
				</select></td>
      		</tr>
		</table>
		
	<%}catch(Exception e){
		error = true;
		message = message +"\n ERROR IS: "+ e.getMessage();
	}%>
		
		download the entry as contextual data spreadsheet: <input class="submitbutton" name="submit" type="submit" value="download filled csv">
		</td></tr>
		<tr align=center><td>
		<input class="submitbutton" name="submit" type="submit" value="proceed">
		</td></tr>
	</table>
	</center>
</form>
</div>
</td>
</tr>
<tr bgcolor="#ffffff"><td align="right" height=30>
	<img height="20" width="200" alt="CDinFusion Step 3" src="/CDinFusion/img/step3.png">
</td></tr>
<jsp:include page="documentation/bottom_info.jsp"></jsp:include>
</table>
</body>
</html>